package com.hgw.Demo07_FlyweightPattern.examples;

import java.util.Hashtable;

/**
 * Data time:2021/12/15 19:30
 * StudentID:2019112118
 * Author:hgw
 * Description: 围棋棋子工厂类，充当享元工厂类，使用单例模式对其进行设计 。
 */
public class IgoChessmanFactory {
    private static IgoChessmanFactory instance = new IgoChessmanFactory();
    private static Hashtable ht;    // 使用HashTable来存储享元对象，充当享元池

    private IgoChessmanFactory() {
        ht = new Hashtable();
        IgoChessman black,white;
        black = new BlackIgoChessman();
        ht.put("b", black);
        white = new WhiteIgoChessman();
        ht.put("w", white);
    }

    // 返回享元工厂类的唯一实例
    public static IgoChessmanFactory getInstance() {
        return instance;
    }

    // 通过key获取存储在HashTable中的享元对象
    public static IgoChessman getIgoChessman(String color) {
        return (IgoChessman) ht.get(color);
    }
}
